cmake_minimum_required(VERSION 3.20)

# 设置项目名称
project(Exception
        LANGUAGES CXX
        VERSION 0.1.0)

# 添加共享库
add_library(${PROJECT_NAME} SHARED ./SGException.cpp)

# 预定义宏
target_compile_definitions(${PROJECT_NAME} PRIVATE EXPORTING_${PROJECT_NAME})

if(UNIX)
  find_package(Boost CONFIG COMPONENTS stacktrace_backtrace)
  if(boost_stacktrace_backtrace_FOUND)
    target_link_libraries(${PROJECT_NAME} PRIVATE Boost::stacktrace_backtrace)
  else()
    message(STATUS "Boost stacktrace_backtrace not found, trying stacktrace_addr2line...")
    find_package(Boost CONFIG COMPONENTS stacktrace_addr2line)
    if(boost_stacktrace_addr2line_FOUND)
      message(STATUS "Boost stacktrace_addr2line found, use it instead.")
      target_link_libraries(${PROJECT_NAME} PRIVATE Boost::stacktrace_addr2line)

    else()
      message(FATAL_ERROR "Boost stacktrace components not found. Please install Boost with stacktrace support.")
    endif()
  endif()
else()
  import_libbacktrace()
  find_package(Boost REQUIRED CONFIG COMPONENTS stacktrace_backtrace)
  target_link_libraries(${PROJECT_NAME} PRIVATE Boost::stacktrace_backtrace)
endif()

if(boost_stacktrace_backtrace_FOUND)
  target_compile_definitions(${PROJECT_NAME} PRIVATE BOOST_STACKTRACE_USE_BACKTRACE)
elseif(boost_stacktrace_addr2line_FOUND)
  target_compile_definitions(${PROJECT_NAME} PRIVATE BOOST_STACKTRACE_USE_ADDR2LINE)
endif()

# Header files install
install(
  DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  DESTINATION include/Utility
  FILES_MATCHING
  PATTERN "*.h"
  PATTERN "UnitTest" EXCLUDE)
  
# 安装目标
install(
  TARGETS ${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin)

# 添加单元测试子项目
if(SG_BUILD_UNIT_TEST)
    add_subdirectory(UnitTest)
endif()
